import React, { Component } from 'react';
import { Select } from 'antd';
import Chooser from 'components/chooser';
import { messages } from 'share/common';

const { Option } = Select;

class MileagePriority extends Component {
  state = {};

  static propTypes = {
    type: React.PropTypes.string, // chooser type
    options: React.PropTypes.array, // Select options
    labelKey: React.PropTypes.string, // chooser labelKey
    valueKey: React.PropTypes.string, // chooser valueKey
    disabled: React.PropTypes.bool,
    value: React.PropTypes.object,
    priority: React.PropTypes.any
  };

  static  defaultProps = {
    disabled: false
  };

  handleSelect = (ret) => {
    const { value = {} } = this.props;
    value.applicationUserGroupScope = ret;
    if (ret === 1001) {
      this.props.onChange({ applicationUserGroupScope: ret });
      return;
    }
    this.props.onChange(value);
  };


  render() {
    let placeholder = '', groupParams = {};
    const { value = {}, options, onChange, type, labelKey, valueKey, disabled, priority } = this.props;
    value.applicationUserGroupScope === 1001 ? placeholder = messages('expense-12.key125')/*通用*/ : placeholder = messages('common.select')/*请选择*/;
    if (priority) {
      groupParams.levelCode = priority.levelCode;
      groupParams.levelOrgId = priority.levelOrgId;
    }
    return (
      <section className="mileage-priority">
        <section className="mileage-priority__select">
          <Select
            value={value.applicationUserGroupScope}
            disabled={disabled}
            onChange={this.handleSelect}
            defaultActiveFirstOption={true}
          >
            {options.map(item => <Option key={item.key} value={item.key}>{item.name}</Option>)}
          </Select>
        </section>
        <section className="mileage-priority__chooser">
          <Chooser
            type={type}
            value={value.optionalUserGroup}
            disabled={value.applicationUserGroupScope === 1001 || disabled}
            valueKey={valueKey}
            labelKey={labelKey}
            onChange={innerValue => onChange({ ...value, optionalUserGroup: innerValue })}
            showNumber={value.applicationUserGroupScope !== 1001}
            placeholder={placeholder}
            listExtraParams={groupParams}
          />
        </section>
      </section>
    );
  }
}

export default MileagePriority;
